﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using NumericalData.Interpolation.Data;
using NumericalData.Interpolation.Data.Methods;
using WPFSolution.CommonOperations;
using WPFSolution.UserControls;

namespace WPFSolution
{
    /// <summary>
    /// Interaction logic for InterpolationWindow.xaml
    /// </summary>
    public partial class InterpolationWindow : Window
    {

        public InterpolationWindow()
        {
            InitializeComponent();
            funcEditor.FunctionDataCreated += CreateGraphTabsByExpression;
        }

        public void CreateGraphTabsByExpression(ExpressionContainer container)
        {
            busyIndic.IsBusy = true;
            var newTab =
                new CloseableTabItem()
                    {
                        Content = new FunctionsDrawer(container,new StirlingMethod()),
                        Header = String.Format("{0}\n{1} : {2}\npts:{3},ordr:{4}",
                        container.Expression,
                        container.IntervalStart,
                        container.IntervalStop,
                        container.Grid.Count,
                        container.MethodOrder)
                    };
            newTab.CloseTab += CloseableTabClose;
            mainTabControl.Items.Add(newTab);
            busyIndic.IsBusy = false;
        }

        public void CloseableTabClose(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show("Closing");
            var tab = (CloseableTabItem)sender;
            mainTabControl.Items.Remove(tab);
        }

        //public void CreateGraphTabsByExpression(ExpressionContainer container)
        //{
        //    _worker.DoWork += _worker_DoWork;
        //    _currentContainer = container;
        //    _worker.RunWorkerCompleted += _worker_RunWorkerCompleted;
        //    _worker.RunWorkerAsync(container);

        //}

        //void _worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        //{
        //    if (e.Error != null)
        //    {
        //        MessageBox.Show("Error Occured", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        //    }
        //    else
        //    {
        //        var newTab =
        //            new CloseableTabItem()
        //            {
        //                Content = (FunctionsDrawer)e.Result,
        //                Header = _currentContainer.Expression
        //            };
        //        newTab.CloseTab += CloseableTabClose;
        //        mainTabControl.Items.Add(newTab);
        //    }
        //}

        //void _worker_DoWork(object sender, DoWorkEventArgs e)
        //{
        //    e.Result = CreateFunctionGraphic(e.Argument);
        //}

        //private FunctionsDrawer CreateFunctionGraphic(object obj)
        //{
        //    var container = (ExpressionContainer)obj;
        //    FunctionsDrawer drawer = new FunctionsDrawer(container);

        //    return drawer;
        //}

        //public void CloseableTabClose(object sender, RoutedEventArgs e)
        //{
        //    //MessageBox.Show("Closing");
        //    var tab = (CloseableTabItem)sender;
        //    mainTabControl.Items.Remove(tab);
        //}
    }
}
